package com.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.beans.FollowRecord;
import com.beans.HouseFavorite;
import com.beans.HouseInfo;
import com.beans.page.PageList;
import com.commons.util.Constants;
import com.data.mapper.BaseMapper;
import com.data.mapper.FollowRecordMapper;
import com.data.mapper.HouseFavoriteMapper;
import com.data.mapper.HouseInfoMapper;
import com.services.HouseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/1/16
 */
@Service
public class HouseInfoServiceImpl extends BaseServiceImpl<HouseInfo> implements HouseInfoService {
    @Autowired
    private HouseInfoMapper houseInfoMapper;
    @Autowired
    private FollowRecordMapper followRecordMapper;
    @Autowired
    private HouseFavoriteMapper houseFavoriteMapper;

    @Override
    public BaseMapper<HouseInfo> getMapper(){return houseInfoMapper; }

    /**
     * 重写房源信息分页查询
     *
     * @param pageList
     * @param houseInfo
     * @return
     */
    public PageList<HouseInfo> queryPageByHouseInfo(PageList<HouseInfo> pageList, HouseInfo houseInfo) {
        Page<HouseInfo> page;
        int pageStart;
        if (pageList.getiDisplayLength() == -1) {//不分页
            pageStart = 1;
            page = PageHelper.startPage(pageStart, 10000, false);
        } else {
            pageStart = pageList.getiDisplayStart() / pageList.getiDisplayLength() + 1;
            page = PageHelper.startPage(pageStart, pageList.getiDisplayLength());
        }
        getMapper().queryList(houseInfo);
        pageList.setiTotalRecords(page.getTotal());
        pageList.setiTotalDisplayRecords(pageList.getiTotalRecords());

        List<HouseInfo> houseInfos = new ArrayList<HouseInfo>();

        if(page.size() > 0){

            for(HouseInfo h : page){
                //取最后一次跟进时间
                FollowRecord followRecord = new FollowRecord();

                followRecord.setRelationId(h.getHouseId());
                followRecord.setRelationType(Constants.oneLong);
                //跟进记录
                List<FollowRecord> followRecords = followRecordMapper.queryList(followRecord);
                if(followRecords.size() > 0){
                    //往房源里存最后一次跟进时间
                    h.setFollowLastDate(followRecords.get(0).getCreateDate());
                }
                //查询询是否收藏
                HouseFavorite houseFavorite = new HouseFavorite();
                houseFavorite.setHouseId(h.getHouseId());
                houseFavorite.setCreateBy(houseInfo.getUpdateBy());
                List<HouseFavorite> houseFavorites = houseFavoriteMapper.queryList(houseFavorite);

                if(houseFavorites.size() > 0){
                    h.setIsCollection(houseFavorites.get(0).getStaus());
                }

                houseInfos.add(h);
            }
        }

        pageList.setAaData(houseInfos);
        return pageList;
    }

    @Override
    public List<HouseInfo> queryExists(HouseInfo houseInfo){
        return houseInfoMapper.queryExists(houseInfo);
    }
}
